package com.yyplatform.admin.platform.notice.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.infra.web.shiro.CurrentUserHolder;
import com.yyplatform.admin.platform.notice.model.dto.SysNoticeUserDTO;
import com.yyplatform.admin.platform.notice.model.dto.SysNoticeUserQueryDTO;
import com.yyplatform.admin.platform.notice.service.SysNoticeUserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/24 22:50
 */
@Slf4j
@Tag(name = "通知/我的通知管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/noticeUser/")
public class SysNoticeUserController {

    private SysNoticeUserService sysNoticeUserService;

    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:noticeUser:query")
    public RestResponse<Page<SysNoticeUserDTO>> page(SysNoticeUserQueryDTO queryDTO) {
        log.debug("query dto = {}", queryDTO);


        Page<SysNoticeUserDTO> page = sysNoticeUserService.page(CurrentUserHolder.getUserId(), queryDTO);


        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:noticeUser:get")
    public RestResponse<SysNoticeUserDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysNoticeUserDTO dto = sysNoticeUserService.get(CurrentUserHolder.getUserId(), id);


        return RestResponse.ok(dto);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:noticeUser:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);
        sysNoticeUserService.delete(CurrentUserHolder.getUserId(), id);

        return RestResponse.ok();
    }


    @PostMapping("deleteAll")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:noticeUser:deleteAll")
    public RestResponse<Void> deleteAll() {

        sysNoticeUserService.deleteAll(CurrentUserHolder.getUserId());

        return RestResponse.ok();
    }


    @PostMapping("updateReadFlag")
    @Operation(summary = "更新已读状态")
    @RequiresPermissions("sys:noticeUser:updateReadFlag")
    public RestResponse<Void> updateReadFlag(String ids) {
        log.debug("updateReadFlag by ids is {}", ids);
        sysNoticeUserService.updateReadFlag(CurrentUserHolder.getUserId(), ids, true);

        return RestResponse.ok();
    }

}
